package com.dy.org.controller;

import com.dy.basic.annotation.PreAuthorize;
import com.dy.basic.util.AjaxResult;
import com.dy.basic.util.PageList;
import com.dy.org.domain.Department;
import com.dy.org.domain.Employee;
import com.dy.org.query.DepartmentQuery;
import com.dy.org.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    /**
     * 查询所有的信息
     */
    @GetMapping
    public List<Department> findAll(){
        return departmentService.findAll();
    }

/*    *//**
     * 根据id查询用户的信息
     *//*
    @GetMapping("/{id}")
    public Department findById(@PathVariable("id") Long id){
        return departmentService.findById(id);
    }*/

    /**
     * 修改或者添加
     */
    @PutMapping
    public AjaxResult UpOrAdd(@RequestBody Department department){
            if (department.getId() == null){
                departmentService.add(department);
                return AjaxResult.me().success();
            } else {
                departmentService.update(department);
                return AjaxResult.me().success();
            }
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
            departmentService.delete(id);
            return AjaxResult.me().success();
    }

    /**
     * 批量删除
     */
    @PatchMapping
    public AjaxResult deleteAll(@RequestBody List<Long> ids){
            departmentService.deleteAll(ids);
            return AjaxResult.me().success();
    }

    /**
     * 分页查询
     */
    @PostMapping
    @PreAuthorize(name = "分页",sn = "department:list")
    public PageList<Department> queryByPage(@RequestBody DepartmentQuery query){
        return departmentService.queryByPage(query);
    }

    /**
     * 获取上级领导
     */
    @GetMapping("/tree")
    public List<Department> getBoos(){
        return departmentService.findDepartmentTree();
    }

    /**
     * 查询所有的员工
     */
    @GetMapping("/findEmp/{id}")
    public List<Employee> findEmp(@PathVariable("id") Long id){
        return departmentService.findEmpById(id);
    }
}
